import request from '@/utils/request.js'
import axios from 'axios';

// 获取举报列表数据
export function getReports(params) {
  return request({
    url: '/reports/getReports',
    method: 'get',
    params
  })
}

// 导出举报数据
export function exportReports(params) {
  return request({
    url: '/reports/exportReports',
    method: 'get',
    responseType: 'blob',
    timeout: 300000, // 增加到5分钟
    params,
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  });
}

// 获取举报详情
export function getReportDetail(reportId) {
  return request({
    url: `/reports/getReportDetail/${reportId}`,
    method: 'get'
  })
}

// 添加下载文件的API
export const downloadFile = (url, maxRetries = 3) => {
  // 实现带重试机制的下载
  const download = (retryCount = 0) => {
    return request({
      url: `/reports/file/download`,
      method: 'get',
      timeout: 300000, // 增加到5分钟
      params: { url },
      responseType: 'blob'  // 重要：指定响应类型为blob
    }).catch(error => {
      // 如果是超时错误且未达到最大重试次数，则重试
      if (retryCount < maxRetries) {
        console.log(`下载重试 ${retryCount + 1}/${maxRetries}`);
        return download(retryCount + 1);
      }
      // 超过最大重试次数，抛出错误
      return Promise.reject(error);
    });
  };
  
  return download();
}

// 增强版导出报告，带重试功能
export function exportReportsWithRetry(params, maxRetries = 3) {
  // 实现带重试机制的导出
  const tryExport = (retryCount = 0) => {
    return request({
      url: '/reports/exportReports',
      method: 'get',
      responseType: 'blob',
      timeout: 300000, // 增加到5分钟
      params,
      headers: {
        'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      }
    }).catch(error => {
      // 如果是超时错误且未达到最大重试次数，则重试
      if (retryCount < maxRetries) {
        console.log(`导出重试 ${retryCount + 1}/${maxRetries}`);
        return tryExport(retryCount + 1);
      }
      // 超过最大重试次数，抛出错误
      return Promise.reject(error);
    });
  };
  
  return tryExport();
}
